<?php session_start(); ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Parent Informer RCSHS - Sending Mail...</title>

<link rel=stylesheet href="../../css-layouts/themes/defaulttheme.css" type="text/css">
<link rel=stylesheet href="../../css-layouts/headlinks.css" type="text/css">

</head>

<body>

<div id="mainhead"><br><br><br></div>
<hr><br>

<?php 

 require_once("../../sqlconfig.php");
 require_once("Mail.php");
 require_once("Mail\mime.php");
 require_once("../lib/sha256.php");

 $testsite=@fsockopen("www.gmail.com", 80);
 if(!$testsite) {
  header("Location: net_err.php");
  die();
 }

 mysql_connect($server, $sqlusr, $sqlpass) or die(mysql_error()); 
 mysql_select_db($mydb) or die(mysql_error()); 

 //Checks if there is a login cookie
 if(isset($_COOKIE["parentinformerprcshs"]) || isset($_COOKIE["parentinformerurcshs"])) { 
  if(isset($_COOKIE["parentinformerprcshs"])) {
   $username = $_COOKIE["parentinformerprcshs"]; 
  }
  else {
   $_SESSION['theerrcode'] = 2;
   header("Location: ../logerr_atmpt.php");
  }
  $pass = $_COOKIE["parentinformerurcshs"];
  $check = mysql_query("SELECT * FROM logtbl WHERE usernm = '".$username."'") or die(mysql_error());
  while($info=mysql_fetch_array($check)) {
   if ($pass!=$info['pwrnd']) {
    $_SESSION['theerrcode'] = 2;
    header("Location: ../logerr_atmpt.php");
   }
   else {

    if($_SESSION['mailrefercode']>2) {
     echo "You have an illegal session cookie value. Your IP address will be recorded to remotely deter<br>";
     echo "malicious events. Please bear with us. Thank you!<br>";
     die();
    }

	// E-mail address validation
    if (!preg_match("/^[^@]*@[^@]*\.[^@]*$/", $_POST['thetohead']) || !preg_match("/^[^@]*@[^@]*\.[^@]*$/", $_POST['thefromhead'])) {
     echo "You have entered an invalid e-mail address. Please click the back<br>";
	 echo "button of your browser to change your e-mail.";
	 die();
    }
    else {

     /*
      * This next block "sanitizes" text in the body part
	  * of the e-mail by converting special characters to
	  * their HTML entities and newlines (\n) to HTML line
	  * breaks.
      */
     $thebody = nl2br(htmlspecialchars($_POST['mainmail']));

	 // Create a new mail_mime class
	 $themsg = new Mail_mime();
     $from = $_POST['thetohead'];
     $to = $_POST['thefromhead'];
     $subject = $_POST['headme'];

	 /*
      * The statements try to send the mail typed in the
      * previous page by using SMTP access (to GOOGLE,
      * WINDOWS LIVE, and [beta!] YAHOO)
      */
     if (preg_match("/^[^@]*@(gmail|googlegroups){1}\.[^@]*$/", $from)) {
      $host = "ssl://smtp.gmail.com";
      $port = "465";
     }
     elseif(preg_match("/^[^@]*@(hotmail|live|msn){1}\.[^@]*$/", $from)) {
      $host = "tls://smtp.live.com";
      $port = "587";
     }
     elseif(preg_match("/^[^@]*@(yahoo|rocketmail|ymail){1}\.[^@]*$/", $from)) {
      $host = "ssl://smtp.mail.yahoo.com";
      $port = "465";
     }
     else { 
      echo "The e-mail domain you have entered is not supported by this application.<br>";
	  echo "Please click the back button of your browser to edit your mail.";
      die();
     }

     $username = $to;
     $password = $_POST['psml']; 

     // Prepare mail headers	 
     $exthd = array ('From' => $from,
                'To' => $to,
                'Subject' => $subject);
				
	 // Arrange SMTP server information
     $smtp = Mail::factory('smtp',
       array ('host' => $host,
         'port' => $port,
         'auth' => true,
         'username' => $username,
         'password' => $password));
 
     // Add an HTML header
     if($_SESSION['mailrefercode'] == 2) {
      $themsg->addHTMLImage(file_get_contents('../../img/themes/default/headers/header2.gif'), 'image/gif', 'header', false); 
	  $htmlbody = "
	  <html>
	  <body style='background-color: #000000;'>
      <img src='header' border=0 alt='Roosevelt College Science High School' width='640'/>
      <br><br>
	  <div style='background-color: #FFFFFF; padding: 50px; border: 5px solid #000000;'>
	  ".$thebody."
	  </div>
	  </body>
	  </html>";
	 
	  $themsg->setHTMLBody($htmlbody);
	 }
	 else {
	  $themsg->setHTMLBody($thebody);
	 }
	 $themsg->setTXTBody(preg_replace('#<br>\s*?/?>#i', '\n', $thebody));
	 $retmsg = $themsg->get();
	 
	 // Send the mail! :D
	 $headers = $themsg->headers($exthd);
     $mail = $smtp->send($to, $headers, $retmsg);
 
     // Check whether the mail was sent successfully
     if (PEAR::isError($mail)) {
	  echo "There was an error in sending the mail. Error details<br>";
	  echo "are written below.<br><br>";
      echo "<b>PEAR::Mail ERROR:</b> " . $mail->getMessage();
      die();
     }
     else {

?>

<center><font id="heading">MAIL SENDING</font></center>
<br><br>
<p align="left">

<?php

     /*
      * A session cookie is set-up to produce the suitable notification
      * once the e-mail made from either index.php or bypassmail.php is
      * passed through here. The session cookie mailrefercode only takes
      * place as two values which was derived from the only two pages where
      * mail can be made.
      *
      * MAILREFERCODE1: The mail passed through here came from index.php, the page
      *                 page which handles general e-mail sending to anonymous
      *                 users. After the mail is sent, the user should be redirected
      *                 to the main page of the site.
      * MAILREFERCODE2: The mail passed through here came from bypassmail.php, the
      *                 page which handles automated mail inquiries for the parents
      *                 of the students. After the mail is sent, the user should be
      *                 redirected to the year level selector.
      */
     if($mail) {
      switch($_SESSION['mailrefercode']) {
       case 1:
        $urlredir="../../";
        echo "The mail you have made was successfully sent to its recipient. You will be<br>";
        echo "redirected to the main page in 5 seconds, or click <a href='".$urlredir."'>here</a>.<br><br>";
        echo "<img src='../../img/prgbar.gif' alt='Progress Bar'><br>";
        break;
       case 2:
        $urlredir="../recd";
        echo "The academic report has been successfully mailed to its receipient. You will be<br>";
        echo "redirected to the year level selector in 5 seconds, or click <a href=".$urlredir.">here</a>.<br><br>";
        echo "<img src='../../img/prgbar.gif' alt='Progress Bar'><br>";
        break;
       default:
        $urlredir="../../";
        echo "It seems that your session cookie was corrupted or removed. However, the mail has<br>";
        echo "been successfully sent to its receipient. You will be redirected to the main page<br>";
		echo "in 5 seconds, or click <a href='".$urlredir."'>here</a>.<br><br>";
        echo "<img src='../../img/prgbar.gif' alt='Progress Bar'><br>";
      }
     }

?>

</p>

<?php

      unset($_SESSION['mailrefercode']);
     }
    }

   }
  }
 }
 else {
  header("Location: ../../");
 }

?>
<br>

<div class="footnav" align=center>

<hr><br>
<span align="center">
&copy; 2011 Roosevelt College Science High School. All rights reserved.
</span>

</div>


<script type="text/javascript">
<!--

/*
 * Count down then redirect script 2
 * By William Kemper (kemperwilliam@hotmail.com)
 * For this script and more, visit http://javascriptkit.com
 *
 * EDIT: This modified version of the script uses AJAX (a method of
 *       website coding where server- and client-side scripting
 *       is done asynchronously) to decide where to redirect.
 */

//specify redirect url
var redirecturl="<?php echo $urlredir; ?>";

//specify pause duration before redirection (in seconds)
var pausefor=5;

//DONE EDITING

function postaction() {
 if (window.timer) {
  clearInterval(timer);
  clearInterval(timer_2);
 }
 window.location=redirecturl;
}

setTimeout("postaction()",pausefor*1000);

//-->
</script>

</body>
</html>